<html>
<head><meta charset="utf-8"><title>Using `SyntaxRewriter` to replace a single node · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Using.20.60SyntaxRewriter.60.20to.20replace.20a.20single.20node.html">Using `SyntaxRewriter` to replace a single node</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="200773993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Using%20%60SyntaxRewriter%60%20to%20replace%20a%20single%20node/near/200773993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Using.20.60SyntaxRewriter.60.20to.20replace.20a.20single.20node.html#200773993">(Jun 13 2020 at 16:28)</a>:</h4>
<p>I'm having a somewhat hard time to understand the code in <code>SyntaxRewriter</code>, but it seems to me that if the rewriter has exactly one replacement, <code>rewrite_root</code> will return the to-be-replaced node, but then calling <code>rewrite</code> with that node won't do anything because that only looks at children. Am I misunderstanding this or is this a bug in <code>SyntaxRewriter</code>?</p>
<p>(<code>AssistBuilder::rewrite</code> uses it in this way and doesn't seem to work correctly because of it)</p>



<a name="200774151"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Using%20%60SyntaxRewriter%60%20to%20replace%20a%20single%20node/near/200774151" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Using.20.60SyntaxRewriter.60.20to.20replace.20a.20single.20node.html#200774151">(Jun 13 2020 at 16:32)</a>:</h4>
<p>Indeed looks like a bug!</p>



<a name="200774177"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Using%20%60SyntaxRewriter%60%20to%20replace%20a%20single%20node/near/200774177" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Using.20.60SyntaxRewriter.60.20to.20replace.20a.20single.20node.html#200774177">(Jun 13 2020 at 16:33)</a>:</h4>
<p>Not suuuper trivial to fix though -- the obvious solution "allow <code>rewrite</code> to rewrite the node itself" doesn't work API wise, as rewriting might turn a single node into a bunch of node</p>



<a name="200774216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Using%20%60SyntaxRewriter%60%20to%20replace%20a%20single%20node/near/200774216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Using.20.60SyntaxRewriter.60.20to.20replace.20a.20single.20node.html#200774216">(Jun 13 2020 at 16:34)</a>:</h4>
<p>So, I think <code>rewrite_root</code> needs to look at lca of "parents" of replacements</p>



<a name="200774250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Using%20%60SyntaxRewriter%60%20to%20replace%20a%20single%20node/near/200774250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Using.20.60SyntaxRewriter.60.20to.20replace.20a.20single.20node.html#200774250">(Jun 13 2020 at 16:35)</a>:</h4>
<p>Hmm, yeah</p>



<a name="200774296"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Using%20%60SyntaxRewriter%60%20to%20replace%20a%20single%20node/near/200774296" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Using.20.60SyntaxRewriter.60.20to.20replace.20a.20single.20node.html#200774296">(Jun 13 2020 at 16:36)</a>:</h4>
<p>Calling <code>.parent().unwrap()</code> on the result of <code>least_common_ancestor</code> seems to return the same node again</p>



<a name="200774306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Using%20%60SyntaxRewriter%60%20to%20replace%20a%20single%20node/near/200774306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Using.20.60SyntaxRewriter.60.20to.20replace.20a.20single.20node.html#200774306">(Jun 13 2020 at 16:36)</a>:</h4>
<p>Oh, because the closure never gets invoked when there's only one replacement</p>



<a name="200774311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Using%20%60SyntaxRewriter%60%20to%20replace%20a%20single%20node/near/200774311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Using.20.60SyntaxRewriter.60.20to.20replace.20a.20single.20node.html#200774311">(Jun 13 2020 at 16:37)</a>:</h4>
<p>seems weird -- parent never returns the node itself</p>



<a name="200774316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Using%20%60SyntaxRewriter%60%20to%20replace%20a%20single%20node/near/200774316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Using.20.60SyntaxRewriter.60.20to.20replace.20a.20single.20node.html#200774316">(Jun 13 2020 at 16:37)</a>:</h4>
<p>right!</p>



<a name="200774324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Using%20%60SyntaxRewriter%60%20to%20replace%20a%20single%20node/near/200774324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Using.20.60SyntaxRewriter.60.20to.20replace.20a.20single.20node.html#200774324">(Jun 13 2020 at 16:37)</a>:</h4>
<p>I think you need <code>.map(|it| it.parent().unwrap_or(it))</code> <em>before</em> the fold</p>



<a name="200774463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Using%20%60SyntaxRewriter%60%20to%20replace%20a%20single%20node/near/200774463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Using.20.60SyntaxRewriter.60.20to.20replace.20a.20single.20node.html#200774463">(Jun 13 2020 at 16:40)</a>:</h4>
<p>Seems to work, thanks!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>